package com.qiaotongtianxia.loans;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.qiaotongtianxia.loans.activity.BaseActivity;
import com.qiaotongtianxia.loans.activity.LoginActivity;
import com.qiaotongtianxia.loans.activity.MainActivity;
import com.qiaotongtianxia.loans.bean.LoginBean;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.SPUtil;
import com.qiaotongtianxia.loans.util.StatusBarUtils;


public class SplashActivity extends BaseActivity {

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        api.setDefaultProgress(null);
        request();
    }
    private void request() {
        api.token(new IBaseRequestImp<String>() {
            @Override
            public void onRequestSuccess(String s) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String info = SPUtil.getString(SplashActivity.this,
                                SPUtil.ACCOUNT_INFO, SPUtil.ACCOUNT_INFO_KEY,"");
                        if (!TextUtils.isEmpty(info)){
                            LoginBean loginBean = new Gson().fromJson(info, LoginBean.class);
                            login(loginBean.getAccount(),loginBean.getPassword());
                        }else{
                            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                },2000);

            }
            @Override
            public void onRequestError(int errorCode, String errorMsg) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                },2000);
            }
        });
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     */
    private void login(final String account, final String password) {
        api.login(account, password, new IBaseRequestImp<Nullable>() {
            @Override
            public void onRequestSuccess(Nullable nullable) {
                Intent intent = new Intent(SplashActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onRequestError(int errorCode, String errorMsg) {
                super.onRequestError(errorCode, errorMsg);
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
    @Override
    public void setStatusBarColor() {
        StatusBarUtils.setWindowStatusBarTrans(this);
    }
}